<!--主页-->
<template>
  <a-layout-content style="padding: 0 50px">
    <a-layout style="padding: 24px 0; background: #fff">
      <a-layout-sider width="200" style="background: #fff">
        <a-menu
            v-model:selectedKeys="selectedKeys2"
            v-model:openKeys="openKeys"
            mode="inline"
            style="height: 100%"
            @click="handleClick"
        >
          <a-menu-item key="welcome">
                <span>
                  <MailOutlined/>
                  欢迎
                </span>
          </a-menu-item>
          <a-sub-menu v-for="item in level1" :key="item.id">
            <template #title>
                <span>
                  <UserOutlined/>
                  {{ item.name }}
                </span>
            </template>
            <a-menu-item v-for="child in item.children" :key="child.id">
              <MailOutlined/>
              {{ child.name }}
            </a-menu-item>
          </a-sub-menu>
        </a-menu>
      </a-layout-sider>
      <a-layout-content :style="{ padding: '0 24px', minHeight: '280px' }">
        <!-- pre 标签就是把内容原封不动按格式输出来 -->
        <!--        <pre>
                  {{ ebooks }}
                  {{books}}
                </pre>-->
        <a-list v-show="!isShowWelcome" item-layout="vertical" size="large" :grid="{ gutter: 20, column: 3}"
                :data-source="ebooks">
          <template #renderItem="{ item }">
            <a-list-item key="item.name">
              <template #actions>
                <span>
                  <component :is="'FileOutlined'" style="margin-right: 8px"/>
                  {{ item.docCount }}
                </span>
                <span>
                  <component :is="'UserOutlined'" style="margin-right: 8px"/>
                  {{ item.viewCount }}
                </span>
                <span>
                  <component :is="'LikeOutlined'" style="margin-right: 8px"/>
                  {{ item.voteCount }}
                </span>
              </template>
              <a-list-item-meta :description="item.description">
                <template #title>
                  <router-link :to="'/doc?ebookId=' + item.id">
                    {{ item.name }}
                  </router-link>
                </template>
                <template #avatar>
                  <a-avatar :src="item.cover"/>
                </template>
              </a-list-item-meta>
            </a-list-item>
          </template>
        </a-list>
        <div class="welcome" v-show="isShowWelcome">
          <theWelcome />
        </div>
      </a-layout-content>
    </a-layout>
  </a-layout-content>
</template>

<script lang="ts">
import {defineComponent, onMounted, ref} from 'vue';
import axios from 'axios'
import {FileOutlined, LikeOutlined, MessageOutlined, MailOutlined, UserOutlined} from '@ant-design/icons-vue';
import {Tool} from "@/util/tool";
import {message} from "ant-design-vue";
import theWelcome from "@/components/the-welcome.vue";


export default defineComponent({
  name: 'HomeView',
  components: {
    FileOutlined,
    LikeOutlined,
    MessageOutlined,
    MailOutlined,
    UserOutlined,
    theWelcome
  },
  setup() {
    const ebooks = ref();
    //const ebooks1 = reactive({books: []})

    // 处理后的树形分类
    const level1 = ref();
    let categorys: any;

    const handleQueryCategory = () => {
      // 这里get传参不用拼接方法 使用params固定参数配置
      axios.get("/category/all").then((response) => {
        const data = response.data;
        if (data.success) {
          categorys = data.content;
          level1.value = [];
          level1.value = Tool.array2Tree(categorys, 0); // 自己处理成树形结构
          console.log("树形结构：", level1.value);
        } else {
          message.error(data.message);
        }
      });
    }

    // 欢迎界面切换
    const isShowWelcome = ref(true);
    var category2Id = 0;
    // 分类导航点击
    const handleClick = (value: any) => {
      console.log("menu click", value);
      if (value.key == "welcome") {
        isShowWelcome.value = true;
      } else {
        category2Id = value.key;
        handleQueryEbook();
        isShowWelcome.value = false;
      }
      // isShowWelcome.value = value.key == "welcome"; // 简写
    }

    const handleQueryEbook = () => {
      axios.get('/ebook/list', {
        params: {
          pageNum: 1,
          pageSize: 1000,
          category2Id: category2Id
        }
      }).then((response) => {
        const data = response.data;
        ebooks.value = data.content.list;
        //ebooks1.books = data.content;
      })
    }
    onMounted(() => {
      handleQueryCategory();
    })

    return {
      ebooks,
      // books: toRef(ebooks1, "books")
      level1,
      handleClick,
      isShowWelcome
    }
  }
});
</script>

<style scoped>
.ant-avatar {
  width: 50px;
  height: 50px;
  line-height: 50px;
  border-radius: 8%;
  margin: 5px 0;
}
</style>